
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="../assets/bootstrap/bootstrap.min.css" rel="stylesheet">
  <script src="../assets/bootstrap/bootstrap.bundle.min.js"></script>
  <title>Document</title>
  <style>
    body {
      font-size: 20px;
    }
    .bgcont{
      padding: 40px 80px;
    }
  </style>
</head>

<body>
  <div id="app" class="bgcont">
    <div class="set">
      <span>元素数量:</span>
      <input type="number" v-model="attrnum">
      <span>行:</span>
      <input type="number" v-model="rownum">
    </div>
    <div>key</div>
    <div class="header">
      <input type="text" v-model="head[i]" v-for="i in attrnum">
    </div>
    <div>value</div>
    <div class="content">
      <div class="controw" v-for="i in rownum">
        {{i}}. <input type="text" v-model="body[i+'_'+j]" v-for="j in attrnum">
      </div>
    </div>
    <div><button @click="createJson" class="btn btn-primary">生成</button></div>
    <div>结果</div>
    <div>{{result}}</div>
  </div>
</body>

</html>

<script type="module">
  import { createApp, ref, onMounted } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'

  createApp({
    setup() {
      const message = ref('Hello Vue!')
      const head = ref([])
      const body = ref([])
      const attrnum = ref(6)
      const rownum = ref(4)
      const result = ref('')

      function createJson() {
        let jsonarr = []
        for (let i = 1; i <= rownum.value; i++) {
          let item = new Object()
          for (let j = 1; j <= attrnum.value; j++) {
            if (head.value[j]) {
              item[head.value[j]] = body.value[i + '_' + j]
            }
          }
          jsonarr.push(item)
        }
        result.value = JSON.stringify(jsonarr)
      }
      onMounted(() => {
      })
      return {
        message,
        head,
        attrnum,
        rownum,
        body,
        result,
        createJson
      }
    }
  }).mount('#app')
</script>
